package cn.com.dhcc.app.pub.core.consts;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPENCLOSE;
import cn.com.dhcc.app.core.util.ReflectionUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst.OBJECT_TYPE;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam.NET_TYPE;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam.NODE_TYPE;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.entity.registry.ExchConnRequest.RequestStatus;
import cn.com.dhcc.turbo.entity.registry.NodePipeline.PIPE_TYPE;
import cn.com.dhcc.turbo.entity.registry.PriorityConf.PRIORITY_LEVEL;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncOperType;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncStatus;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncType;
import cn.com.dhcc.turbo.entity.sysmgr.UserInfo.USER_TYPE;
import cn.com.dhcc.turbo.service.monitor.MonitorQuotaStatic.ALERT_PIPELINE;
import cn.com.dhcc.turbo.service.monitor.MonitorQuotaStatic.ALTER_LEVEL;
import cn.com.dhcc.turbo.service.monitor.MonitorQuotaStatic.QUOTA_TYPE;

/**
 * 码表注册
 * @author qiao
 * @2014-6-5 @下午8:09:22
 */
public class CT {

	@SuppressWarnings("deprecation")
	static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	/**
	 * 在此注册的常量（enum），即可使用如下功能：
	 * 1 {@link AppTagEl#ctoption(String, String)} 来创建下拉框
	 * 2 {@link AppTagEl#ctname(String, String) 根据code获取name
	 */
	public final static Map<String, Class<?>> CodeTablePool = new HashMap<String, Class<?>>();
	
	static{
		/**
		 * 注意：key 不能相同，必须有code和name两个属性
		 */
		try {
			regist(QUOTA_TYPE.class);
			regist(PIPE_TYPE.class);
			regist(SyncType.class);
			regist(SyncStatus.class);
			regist(SyncOperType.class);
			regist(RequestStatus.class);
			regist(ALERT_PIPELINE.class);
			regist(OPENCLOSE.class);
			regist(ALTER_LEVEL.class);
			regist(USER_TYPE.class);
			regist(NET_TYPE.class);
			regist(NODE_TYPE.class);
			regist(PRIORITY_LEVEL.class);
			regist(OBJECT_TYPE.class);
		} catch (Exception e) {
			logger.error("码表注册失败",e);
		}
		
		
		
	};
	/**
	 * 注册码表
	 * @param ctClazz 码表class
	 * @throws InitException 
	 */
	private static void regist(Class<?> ctClazz) throws InitException{
		check(ctClazz);
		CodeTablePool.put(ctClazz.getSimpleName(), ctClazz);
	}
	/**
	 * 验证不能相同，必须有code和name两个属性
	 * @param ctClazz
	 * @throws InitException 
	 */
	private static void check(Class<?> ctClazz) throws InitException{
		String key = ctClazz.getSimpleName();
		if(!(ReflectionUtil.hasField(ctClazz, "code")&&ReflectionUtil.hasField(ctClazz, "name"))){
			throw new InitException("码表["+ctClazz+"]注册失败，enum里面必须包含code和name属性");
		}
		if(CodeTablePool.containsKey(key)){
			throw new InitException("码表["+ctClazz+"]注册失败，键值已被注册");
		}
	}
}
